﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace Rizmar
{
    public partial class PesquisaSacadoForm : PesquisaBaseForm
    {
        Sacado sacadoParent;
        Titulo tituloParent;

        public PesquisaSacadoForm(object parentForm)
        {
            sacadoParent = null;
            tituloParent = null;

            if (parentForm is Titulo)
                tituloParent = (Titulo)parentForm;
            else if (parentForm is Sacado)
                sacadoParent = (Sacado)parentForm;

            InitializeComponent();
        }

        private void PesquisaSacadoForm_Load(object sender, EventArgs e)
        {
            this.sacadoTableAdapter.Fill(this.rizmarDataSet.Sacado);
            radioButtonCnpj.Checked = true;
        }

        private void textBoxBusca_TextChanged(object sender, EventArgs e)
        {
            string selectCommand = "SELECT id, nome,documento FROM Sacado ";
            if (radioButtonNome.Checked)
                selectCommand += "WHERE nome LIKE '" + textBoxBusca.Text + "%'";
            else if (radioButtonCpf.Checked)
                selectCommand += "WHERE documento LIKE '" + textBoxBusca.Text + "%' AND documento LIKE '___.%'";
            else
                selectCommand += "WHERE documento LIKE '" + textBoxBusca.Text + "%' AND documento LIKE '__.%'";

            SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            sacadoBindingSource.DataSource = table;
            this.textBoxBusca.Focus();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (sacadoParent != null)
            {
                string selectString = "SELECT * FROM Sacado WHERE id = " + dataGridView.SelectedRows[0].Cells[0].Value.ToString() + "";
                string[] colunas = new string[] { "id", "nome", "documento", "logradouro", "complemento", "numero", "bairro", "cep", "cidade", "estado", "telefone", "celular", "email", "obs" };
                string[] atributos = new string[14];

                SqlConnection cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                cn.Open();

                using (SqlCommand selectCommand = new SqlCommand(selectString, cn))
                {
                    SqlDataReader dr = selectCommand.ExecuteReader();
                    dr.Read();

                    for (int i = 0; i < 14; i++)
                        atributos[i] = dr[colunas[i]].ToString();
                }
                cn.Close();

                sacadoParent.zerarCampos();
                sacadoParent.setTextboxes(atributos);
            }
            else if (tituloParent != null)
            {
                tituloParent.setSacadoTextBox(dataGridView.SelectedRows[0].Cells[1].Value.ToString());
                tituloParent.setSacadoLabel(dataGridView.SelectedRows[0].Cells[0].Value.ToString());
            }
            this.Close();
        }

        private void radioButtonDocumento_CheckedChanged(object sender, EventArgs e)
        {
            string selectCommand = "SELECT id, nome,documento FROM Sacado ";
            if (radioButtonNome.Checked)
                selectCommand += "WHERE nome LIKE '" + textBoxBusca.Text + "%'";
            else if (radioButtonCpf.Checked)
                selectCommand += "WHERE documento LIKE '" + textBoxBusca.Text + "%' AND documento LIKE '___.%'";
            else
                selectCommand += "WHERE documento LIKE '" + textBoxBusca.Text + "%' AND documento LIKE '__.%'";

            SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            sacadoBindingSource.DataSource = table;
            this.textBoxBusca.Focus();
        }

        private void buttonCancelar_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void novoButton_Click(object sender, EventArgs e)
        {
            Sacado janela = new Sacado();
            janela.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            janela.Show();
        }
    }
}
